package cn.lanchengkeji.datafillingsystem.controller;



import cn.lanchengkeji.datafillingsystem.common.ResponseBean;
import cn.lanchengkeji.datafillingsystem.common.StatusCode;
import cn.lanchengkeji.datafillingsystem.entity.OperationLog;
import cn.lanchengkeji.datafillingsystem.service.IOperationLogService;
import cn.lanchengkeji.datafillingsystem.vo.OperationLogVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户登陆记录 前端控制器
 * </p>
 *
 * @author yixu
 * @since 2020-04-24
 */
@RestController
@RequestMapping("/operation-log")
public class OperationLogController extends BaseController {

    @Resource
    IOperationLogService iOperationLogService;

    @RequiresAuthentication
    @RequiresPermissions("operationLog:view")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResponseBean page(
            @RequestBody OperationLogVO operationLogVO,
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        IPage<OperationLog> page = new Page();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        QueryWrapper<OperationLog> pageWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(operationLogVO.getOperationUserName())) {
            pageWrapper.like(OperationLog.OPERATION_USER_NAME, operationLogVO.getOperationUserName());
        }

        if (StringUtils.isNotBlank(operationLogVO.getRemoteIp())) {
            pageWrapper.like(OperationLog.REMOTE_IP, operationLogVO.getRemoteIp());
        }

        if (StringUtils.isNotBlank(operationLogVO.getOperationMenu())) {
            pageWrapper.like(OperationLog.OPERATION_MENU, operationLogVO.getOperationMenu());
        }

        if (StringUtils.isNotBlank(operationLogVO.getOperationType())) {
            pageWrapper.like(OperationLog.OPERATION_TYPE, operationLogVO.getOperationType());
        }

        if (StringUtils.isNotBlank(operationLogVO.getOperationEvent())) {
            pageWrapper.like(OperationLog.OPARATION_EVENT, operationLogVO.getOperationEvent());
        }

        if (null != operationLogVO && StringUtils.isNotBlank(operationLogVO.getStartTime())) {
            pageWrapper.ge(OperationLog.OPERATION_DATE, operationLogVO.getStartTime().concat("  00:00:00"));
        }

        if (null != operationLogVO && StringUtils.isNotBlank(operationLogVO.getEndTime())) {
            pageWrapper.le(OperationLog.OPERATION_DATE, operationLogVO.getEndTime().concat("  23:59:59"));
        }

        pageWrapper.orderByDesc(OperationLog.OPERATION_DATE);
        page = iOperationLogService.page(page, pageWrapper);
        return new ResponseBean(StatusCode.OK, page);
    }

}
